<?php
class View_Post{
	public static function displayMail($arrChains, $arrUsers, $idUser){
		echo '<div class="allChains">';
		foreach ($arrChains as $arrChain){
			echo '
			<div class="">
			<div class="oneMail">
				<div class="oneMail_page_bottom">
					<div class="pod_left_top">
						<div class="oneMail_page_right">
							<div class="oneMail_page_left">
								<div class="oneMail_page_right_top">
									<div class="oneMail_page_right_bootom">
										<div class="oneMail_page_left_bottom">
											<div class="oneMail_page_left_top">
												<div class="status">';
													if ($arrChain['unread_flag'])
														if ($arrChain['last_message_ID_user'] == $idUser)
															echo '<p title="Сообщения в цепочке ещё не прочитанны вашим собеседником"><img src="' . W_IMAGES . 'link/link_masage.gif" />';
														else{ 
															$issetNewMail = true;
															echo '<p title="Есть непрочитанные сообщения"><img src="' . W_IMAGES . 'link/link_message.gif" />';
														}
												echo '
												</p></div>
												<div class="left">											
														<p>
															<h4 class="fade">
																<a href="' . W_ABSPATH . 'user/' . $arrUsers[$arrChain['ID_first_user']]['login'] . '">' . $arrUsers[$arrChain['ID_first_user']]['login'] . '<i class="fade2"></i></a>
															</h4>
														</p>
														<p style="font-size:14px;">↓</p>															
														<p>
															<h4 class="fade">
																<a href="' . W_ABSPATH . 'user/' . $arrUsers[$arrChain['ID_second_user']]['login'] . '">' . $arrUsers[$arrChain['ID_second_user']]['login'] . '<i class="fade2"></i></a>
															</h4>
														</p>
												</div>
												
												<div class="right count"><p title="сообщений в цепочке">' . $arrChain['count'] . '<p></div>
												
												<div class="right">
													<!--<p><a href="' . W_ABSPATH . 'mail/chain/' . $arrChain['ID_chain'] . '/">открыть</a></p>-->';
			
													if ($arrChain['last_text'])
														echo '
														<p class="first_text" title="' . Service_Date::getCaseDateForUser($arrChain['date_first'], 3, 2, true) . '">' . Service_Date::getCaseDateForUser($arrChain['date_first']) . '</p>
														<p title="' . Service_Date::getCaseDateForUser($arrChain['date_last'], 3, 2, true) . '">' . Service_Date::getCaseDateForUser($arrChain['date_last']) . '</p>';
													else 
														echo '
														<p class="centerDates" title="' . Service_Date::getCaseDateForUser($arrChain['date_first'], 3, 2, true) . '">' . Service_Date::getCaseDateForUser($arrChain['date_first']) . '</p>';		
														
												echo '		
												</div>
												<div class="center">';
													if ($arrChain['last_text'])
														echo '
														<h4 class="fade first_text">
															<a class="noneGray" href="' . W_ABSPATH . 'mail/chain/'  .$arrChain['ID_chain'] . '/' . (isset($issetNewMail) ? '#newMail' : '') . '">' . $arrChain['first_text'] . '<i class="fade"></i></a>
														</h4>
														<h4 class="fade">
															<a class="none" href="' . W_ABSPATH . 'mail/chain/'  .$arrChain['ID_chain'] . '/#lastMessage' . (isset($issetNewMail) ? '#newMail' : '') . '"><span>' . $arrChain['last_text'] . '</span><i class="fade"></i></a>
														</h4>';
													else 
														echo '
														<h4 class="fade  centerP">
															<a class="none" href="' . W_ABSPATH . 'mail/chain/'  .$arrChain['ID_chain'] . '/#lastMessage">' . $arrChain['first_text'] . '<i class="fade"></i></a>
														</h4>';														
												echo '
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			</div>
			';
		}
		echo '</div>';
	}
	
	/**
	 * цепочка
	 */
	public static function displayChain($arrMail, $arrUsers, $idUser, $arrChain, $viewFlag = true){
		$content = '';
		$viewFlag ? $content .= '<div class="allChains">' : '';
		$idLastMail = $arrMail[sizeof($arrMail) - 1]['ID_message'];
		$size = count($arrMail);
		$i = 0;
		foreach ($arrMail as $arrMail){
			$i++;
			if ($i == $size)
				$content .= '<a name="lastMessage"></a>';
			
			$content .= '<table width="100%" cellpadding="0" cellspacing="0">';
				$content .= '<tr>';
					$content .= '<td rowspan="3" class="tableStatus" valign="top">';
						if ($arrMail['unread_flag'])
							if ($arrMail['ID_user'] == $idUser)
								$content .= '<p title="Сообщение не прочитанно вашим собеседником"><img src="' . W_IMAGES . 'link/link_masage.gif" /></p>';
							else 
								$content .= '<a name="newMail"></a><p title="Непрочитанное сообщение"><img src="' . W_IMAGES . 'icons/message.gif" /></p>';
					$content .= '</td>';
					$content .= '<td rowspan="3" class="tableAva" valign="top">';
						$content .= '<a href="' . W_ABSPATH . 'user/' . $arrUsers[$arrMail['ID_user']]['login'] . '"><img src="' . W_IMAGES . 'pictures/user/' . (!empty($arrUsers[$arrMail['ID_user']]['pic_path']) ? ('s_' . $arrMail['ID_user'] . '.' . $arrUsers[$arrMail['ID_user']]['pic_path']) : 's_default.gif') . '" /></a>';
					$content .= '</td>';
					$content .= '<td valign="top" class="topTd">';
						$content .= '<a class="blue" href="' . W_ABSPATH . 'user/' . $arrUsers[$arrMail['ID_user']]['login'] . '">' . $arrUsers[$arrMail['ID_user']]['login'] . '</a> <span class="floatRight" title="' . Service_Date::getCaseDateForUser($arrMail['date'], 3, 2, true) . '">' . Service_Date::getCaseDateForUser($arrMail['date']) . '</span>';
					$content .= '</td>';
				$content .= '</tr>';
				$content .= '<tr>';
					$content .= '<td valign="top" class="messageText">';
						$content .= $arrMail['text'];
					$content .= '</td>';
				$content .= '</tr>';
				$content .= '<tr>';
					$content .= '<td valign="top">';
						$content .= '<a class="js" href="javascript:citata(\'' . $arrMail['text'] . '\', \'' . $arrUsers[$arrMail['ID_user']]['login'] . '\', \'' . Service_Date::getCaseDateForUser($arrMail['date'], 3, 2, true) . '\');">цитировать</a> <!--<a class="js" href="javascript:alert(1)">удалить</a>-->';
					$content .= '</td>';
				$content .= '</tr>';
			$content .= '</table>';
			$content .= '<div class="underBorder"></div>';			
		}
		$viewFlag ? $content .= View_Post::mailForm($idUser == $arrChain['ID_first_user'] ? $arrChain['ID_second_user'] : $arrChain['ID_first_user'], $arrChain['ID_chain'], true, $idLastMail, false) : '';
		$viewFlag ? $content .= '</div>' : '';
		if ($viewFlag)
			echo $content;
		else 
			return $content;
	}
	
		
	/**
	 * форма отправки сообщения
	 */
	public static function mailForm($idUser, $idChain = NULL, $chainPage = 0, $idLastMail = '', $viewFlag = true){
		$content = '';
		$content .= '<div class="userSuccess mailSuccess" style="display: none;">Сообщение отправлено, перейти в <a href="' . W_ABSPATH . 'mail/">сообщения</a></div>';
		$content .= '<form name="messageForm" onkeypress="ctrlEnterMail(event, this.newMessage, ' . $chainPage . ');" id="newMessage" ' . (!$chainPage ? ('style="display: none;"') : '') . 'action="" onsubmit="sendNewMail(this.newMessage, ' . $chainPage . '); return false;">';
		$content .= '<h3>Новое сообщение</h3>';
			$content .= '<textarea name="message" name="text" id="text" cols="52" rows="10"></textarea>';
			$content .= '<p><input type="image" src="' . W_IMAGES . 'button/btn_main_otpravit.gif"/>' . (!$chainPage ? ('<a href="javascript:closeMailForm();">закрыть</a>') : '') . '</p>';
			$content .= '<input type="hidden" name="user" id="user" value="' . $idUser . '" />';
			$content .= '<input type="hidden" name="idChain" id="idChain" value="' . $idChain . '" />';
			$content .= '<input type="hidden" name="idLastMail" id="idLastMail" value="' . $idLastMail . '" />';
		$content .= '</form>';
		if ($viewFlag)
			echo $content;
		else 
			return $content;
	}
	
	/**
	 * вывод заголовка страницы сообщения
	 */
	public static function displayChainHead($arrChain, $arrUsers){
		echo '<div class="mailHead">
						<div class="floatLeft main">
							<p class="vsImageH"><a href="' . W_ABSPATH . 'user/' . $arrUsers[$arrChain['ID_first_user']]['login'] . '"><img align="middle" src="' . W_IMAGES . 'pictures/user/' . (!empty($arrUsers[$arrChain['ID_first_user']]['pic_path']) ? ('vs_' . $arrChain['ID_first_user'] . '.' . $arrUsers[$arrChain['ID_first_user']]['pic_path']) : 'vs_default.gif') . '" /></a></p>
							<p><a href="' . W_ABSPATH . 'user/' . $arrUsers[$arrChain['ID_first_user']]['login'] . '">' . $arrUsers[$arrChain['ID_first_user']]['login'] . '</a></p>
						</div>
						<div class="floatLeft"><p style="font-size:14px;" class="title">→</p></div>
						<div class="floatLeft main">
							<p class="vsImageH"><a href="' . W_ABSPATH . 'user/' . $arrUsers[$arrChain['ID_second_user']]['login'] . '"><img src="' . W_IMAGES . 'pictures/user/' . (!empty($arrUsers[$arrChain['ID_second_user']]['pic_path']) ? ('vs_' . $arrChain['ID_second_user'] . '.' . $arrUsers[$arrChain['ID_second_user']]['pic_path']) : 'vs_default.gif') . '" /></a></p>
							<p><a href="' . W_ABSPATH . 'user/' . $arrUsers[$arrChain['ID_second_user']]['login'] . '">' . $arrUsers[$arrChain['ID_second_user']]['login'] . '</a></p>
						</div>
						<div class="main">	
							<h4 class="fade title">							
								<a class="main" href="' . W_ABSPATH . 'mail/">Сообщения</a>	
								<a class="last" href="#">' . $arrChain['first_text'] . '<i class="fade"></i></a>
							</h4>
						</div>
					</div>';
	}
}
?>